package com.zy.ivideo.service;

import com.zy.ivideo.entity.ReplyEntity;
import com.zy.ivideo.entity.ResponseEntity;
import com.zy.ivideo.repository.CommentRepository;
import com.zy.ivideo.repository.ReplyRepository;
import com.zy.ivideo.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReplyService {
    @Autowired
    ReplyRepository repository;
    @Autowired
    CommentRepository commentRepository;

    /**
     * 回复评论
     * @param entity
     * @return
     */
    public ResponseEntity<ReplyEntity> reply(ReplyEntity entity){
        ReplyEntity result = repository.save(entity);
        if (result!=null){
            int i = commentRepository.modifyReplytotal(result.getId(), 1);
            if (i>0){
                return ResponseUtils.success(result);
            }

        }
        return ResponseUtils.failed("回复失败");
    }

    /**
     * 删除回复
     * @param id
     * @return
     */
    public ResponseEntity<Boolean> removeReply(Long id){
        try{
            repository.deleteById(id);
            int i = commentRepository.modifyReplytotal(id, -1);
            if (i>0){
                return ResponseUtils.success(true);
            }

        }catch (Exception ex){
            return ResponseUtils.failed("删除回复失败");
        }
        return ResponseUtils.failed("删除回复失败");
    }

    /**
     * 根据评论id获取回复信息
     * @param commentid
     * @return
     */
    public ResponseEntity<List<ReplyEntity>> getReplyByCommentId(long commentid){
        List<ReplyEntity> result = repository.getReplyByCommentId(commentid);
        if (result!=null&&result.size()>0){
            return ResponseUtils.success(result);
        }
        return ResponseUtils.failed("没有获取到回复信息");
    }

    /**
     * 点赞
     * @param replyid
     * @return
     */
    public ResponseEntity<Boolean> agree(int replyid){
        int result = repository.modifyAgreeNum(replyid, 1);
        if (result>0){
            return ResponseUtils.success(true);
        }
        return ResponseUtils.failed("点赞失败");
    }

    /**
     * 取消点赞
     * @param replyid
     * @return
     */
    public ResponseEntity<Boolean> unAgree(int replyid){
        int result = repository.modifyAgreeNum(replyid, -1);
        if (result>0){
            return ResponseUtils.success(true);
        }
        return ResponseUtils.failed("取消点赞失败");
    }
}
